﻿#include "combinesplitwidget.h"
#include "ui_combinesplitwidget.h"
#include "readonlydelegate.h"

CombineSplitWidget::CombineSplitWidget(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CombineSplitWidget)
{
    ui->setupUi(this);

    model = new NewFormulaStandardItemModel();
    ui->tableView->setModel(model);
    formula = nullptr;
    currentTotal = 0;
    ui->lineEditCombineSplitCount->setText("1");

    QPalette bluePaletteLabel;
    bluePaletteLabel.setColor(QPalette::WindowText, Qt::blue);
    QPalette bluePaletteLineEdit;
    bluePaletteLineEdit.setColor(QPalette::Text, Qt::blue);

    ui->labelCount->setPalette(bluePaletteLabel);
    ui->lineEditCount->setPalette(bluePaletteLineEdit);
    ui->labelCombineSplitCount->setPalette(bluePaletteLabel);
    ui->lineEditCombineSplitCount->setPalette(bluePaletteLineEdit);

    connect(&productService, &ProductService::getFormulaInfoSuccess,
            [=](Formula *formula)
    {
        if(formula != nullptr){
            this->initWithFormula(formula);
        }
    });
}

CombineSplitWidget::~CombineSplitWidget()
{
    delete ui;
}

void CombineSplitWidget::setWidgetTpe(WidgetType type){
    widgetType = type;
    QString typeName;
    if(type == WidgetType::CombineWidget){
        typeName = "装配";
        ui->labelCombineSplitCount->setText("装配份数：");
        ui->labelBalance->hide();
        ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
        model->setWidgetType(NewFormulaStandardItemModel::CombineProduct);
    }else{
        model->setWidgetType(NewFormulaStandardItemModel::SplitProduct);
        for(int i=0;i<model->columnCount();i++){
            if(i!=3)
                ui->tableView->setItemDelegateForColumn(i, new ReadOnlyDelegate());
        }
        ui->labelCombineSplitCount->setText("拆分份数：");
        typeName = "拆分";
    }

    setWindowTitle(typeName);
    ui->labelTypeName->setText(typeName);
    ui->pushButtonSubmit->setText(typeName);
}

void CombineSplitWidget::initWithCombineId(QString combineId){
    productService.getFormulaInfo(this, combineId);
}

void CombineSplitWidget::initWithFormula(Formula *formula){
    ui->lineEditCombineSplitCount->setText("1");
    this->formula = formula;
    Product *combineProduct = formula->getCombineProduct();
    QList<Product*> splitProducts = formula->getSplitProducts();

    if(widgetType == WidgetType::CombineWidget){
        connect(model, &NewFormulaStandardItemModel::updateTotal,
                [=](double total)
        {
            currentTotal = total;
            ui->lineEditInPrice->setText(QString::number(total/combineProduct->count, 'f', 2));
        });
    }else{
        connect(model, &NewFormulaStandardItemModel::updateTotal,
                [=](double total)
        {
            currentTotal = total;
            this->refreshBalance();
        });
    }

    ui->lineEditType->setText(combineProduct->type);
    ui->lineEditName->setText(combineProduct->name);
    ui->lineEditUnit->setText(combineProduct->unit);
    ui->lineEditCount->setText(QString("%1").arg(combineProduct->count));
    ui->lineEditInPrice->setText(QString::number(combineProduct->inPrice, 'f', 2));

    model->reset();
    model->appendData(splitProducts);
    exec();
}

void CombineSplitWidget::on_pushButtonExit_clicked()
{
    close();
}

void CombineSplitWidget::refreshBalance()
{
    if(formula != nullptr){
        int combineSplitCount = ui->lineEditCombineSplitCount->text().toInt();
        double balance = formula->getCombineProduct()->inPrice*formula->formulaBase->combineCount-currentTotal;
        ui->labelBalance->setText(QString("差额：%1*%2=%3")
                                  .arg(combineSplitCount)
                                  .arg(QString::number(balance, 'f', 2))
                                  .arg(QString::number(combineSplitCount*balance, 'f', 2)));
    }
}

void CombineSplitWidget::on_pushButtonSubmit_clicked()
{
    if(!userService.inputOperatePassword(this)){
        return;
    }

    if(widgetType == WidgetType::CombineWidget){
        productService.combineProduct(this, ui->pushButtonSubmit, formula, ui->lineEditCombineSplitCount->text().toInt());
    }else{
        productService.splitProduct(this, ui->pushButtonSubmit, formula, ui->lineEditCombineSplitCount->text().toInt(), model->getAllInPrices());
    }
}

void CombineSplitWidget::on_lineEditCombineSplitCount_textChanged(const QString &text)
{
    if(!text.isEmpty()){
        int combineSplitCount = text.toInt();
        if(combineSplitCount<1)combineSplitCount=1;
        ui->lineEditCombineSplitCount->setText(QString("%1").arg(combineSplitCount));
        refreshBalance();
    }
}
